package com.dnjn.mes.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dnjn.mes.biz.assist.ext.Wrapperz;
import com.dnjn.mes.biz.domain.MesAssembly;
import com.dnjn.mes.biz.domain.MesEquipment;
import com.dnjn.mes.biz.domain.MesWorkpiece;
import com.dnjn.mes.biz.service.IMesAssemblyService;
import com.dnjn.mes.common.core.controller.BaseController;
import com.dnjn.mes.common.core.domain.AjaxResult;
import com.dnjn.mes.common.utils.StringUtils;
import com.dnjn.mes.common.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * <p>
 * 组件|模块表 前端控制器
 * </p>
 *
 * @author Amber
 * @since 2023-07-26
 */
@Tag(name = "mes assembly 模块", description = "模块")
@RestController
@RequestMapping("/biz/mesAssembly")
public class MesAssemblyController extends BaseController {

    @Autowired
    protected IMesAssemblyService baseService;

    @GetMapping("/fetchByEquipmentId/{equipmentId}")
    public AjaxResult fetchByEquipmentId(@PathVariable Long equipmentId) {
        List<MesAssembly> list = baseService.lambdaQuery()
                .eq(MesAssembly::getEquipmentId, equipmentId)
                .eq(MesAssembly::getDeleted,"0")
                .orderByAsc(MesAssembly::getOrdinal)
                .list();
        return success(list);
    }

    @GetMapping("/get/{id}")
    public AjaxResult get(@PathVariable Long id) {
        return success(baseService.getById(id));
    }

    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody MesAssembly domain) {
        long count = baseService.count(
                Wrappers.lambdaUpdate(MesAssembly.class).eq(MesAssembly::getAssemblyName, domain.getAssemblyName()));
        Assert.isTrue(count == 0, "mes.assembly.exist");
        return toAjax(baseService.save(domain));
    }

    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody MesAssembly domain) {
        return toAjax(baseService.updateById(domain));
    }

    @DeleteMapping("/{assemblyId}")
    public AjaxResult remove(@PathVariable Long assemblyId) {
        if (Objects.isNull(assemblyId)) {
            return error("assemblyId不能为空！");
        }
//        baseService.removeBatchByIds(Arrays.asList(assemblyId));
       Boolean flag = baseService.removeAssembly(assemblyId);
       if (flag){
           return success("删除成功");
       }else {
           return error("删除失败，请检查是否关联工件或异常！");
       }
    }


    @PostMapping("/export")
    public void export(HttpServletResponse response, MesAssembly domain) {
        List<MesAssembly> list = baseService.list(Wrappers.query(domain));
        ExcelUtil<MesAssembly> util = new ExcelUtil<MesAssembly>(MesAssembly.class);
        util.exportExcel(response, list, "参数数据");
    }

    @Operation(summary ="添加模块")
    @PostMapping("/addModule")
    public AjaxResult add(@RequestParam(required = true) Long equipmentId, @RequestParam(required = true) String assemblyName) {
        if(StringUtils.isEmpty(assemblyName)){
            return error("模块名称不能为空！");
        }
        MesAssembly mesAssembly = new MesAssembly();
        mesAssembly.setEquipmentId(equipmentId);
        mesAssembly.setAssemblyName(assemblyName);
        long cnt = baseService.count(Wrapperz.lambdaQuery(MesAssembly.class).eq(MesAssembly::getAssemblyName, assemblyName));
        Assert.isTrue(cnt == 0, "mes.mesAssembly.exist");
        //获取最大的ordinal值
        Integer ordinal = baseService.getMaxOrdinal();
        ordinal++;
        mesAssembly.setOrdinal(ordinal);
        boolean flag = baseService.save(mesAssembly);
        return toAjax(flag);

    }

}